package com.will.txj.aj.security.config.auth;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: wen-yi;
 * @date: 2021/12/5 22:07;
 * @Description: 注销操作
 */
@Slf4j
@Component("myLogoutHandler")
public class MyLogoutHandler implements LogoutHandler {
    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String headerToken = request.getHeader("Authorization");
        log.info("logout header Token : {}", headerToken);
        log.info("logout request getMethod : {}", request.getMethod());
        if (!StringUtils.isEmpty(headerToken)){
            // postMan测试时，自动加入的前缀，要去掉
            String token = headerToken.replace("Bearer ", "").trim();
            log.info("authentication : {}", authentication);
            SecurityContextHolder.clearContext();
        }
    }
}
